﻿Imports EstoqueBLL
Public Class FabricanteUC
    Dim objBLL As New FabricanteBLL
#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click
        Try
            Dim cad As New FabricanteCAD
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("fab_codigo") Is Nothing) Then
                    objBLL = New FabricanteBLL(Val(DGVDados.Rows(linha.Index).Cells("fab_codigo").Value.ToString()))
                End If
            Next
            If (objBLL.fab_codigo > 0) Then
                cad.lcodigo.Text = objBLL.fab_codigo
                cad.lcodigo.Visible = False
                cad.Label1.Visible = True
                cad.LTitulo.Text = objBLL.fab_descricao
                TSBAlterar.BackColor = Color.Gainsboro
                cad.ShowDialog()
                CBFiltro_SelectedIndexChanged(Nothing, Nothing)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Cadastrar"
    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        Try
            Dim cad As New FabricanteCAD
            TSBCadastrar.BackColor = Color.Gainsboro
            cad.ShowDialog()
            CBFiltro_SelectedIndexChanged(Nothing, Nothing)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBCadastrar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Carrega Grid"
    Public Sub carregagrid()
        Dim dt As New DataTable
        dt = objBLL.localizar("", 9)
        DGVDados.DataSource = dt
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Click da Grid"
    Private Sub DGVDados_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        objBLL = Nothing
        If (e.RowIndex >= 0) Then
            objBLL = New FabricanteBLL(Val(DGVDados.Rows(e.RowIndex).Cells(0).Value))
        End If
    End Sub
#End Region

#Region "Combobox Filtro"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        TXTLocalizar.Clear()
        If (CBFiltro.Text = "Status") Then
            TXTLocalizar.Visible = False
            CBStatus.Visible = True
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(Nothing, Nothing)
        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
            CBStatus.Visible = False
        End If
        If TXTLocalizar.Text = "" Then
            carregagrid()
        End If
    End Sub
#End Region

#Region "Combobox Status"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        Dim sta As Integer = 0
        If (CBStatus.Text = "Ativo") Then sta = 1
        If (CBStatus.Text = "Inativo") Then sta = 2
        DGVDados.DataSource = objBLL.localizar(sta, 3)
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("fab_codigo") Is Nothing) Then
                    objBLL = New FabricanteBLL(Val(DGVDados.Rows(linha.Index).Cells("fab_codigo").Value.ToString()))
                End If
            Next
            If (objBLL.fab_codigo > 0) Then
                TSBExcluir.BackColor = Color.Gainsboro
                If (MessageBox.Show("Confirma a exclusão do fabricante: " & objBLL.fab_descricao & "?", "Exclusão", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
                    objBLL.sta_codigo = 3
                    objBLL.alterar(objusuarios.Codigo)
                    CBFiltro_SelectedIndexChanged(Nothing, Nothing)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBExcluir.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "KeyDown Form"
    Private Sub FabricanteUC_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            TSBVoltar_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "Localizar"
    Private Sub TSBBuscar_Click(sender As System.Object, e As System.EventArgs) Handles TSBBuscar.Click
        If (PLocalizar.Visible) Then
            PLocalizar.Visible = False
            TSBBuscar.BackColor = Color.Transparent
            carregagrid()

        Else
            PLocalizar.Visible = True
            TSBBuscar.BackColor = Color.Gainsboro
        End If
        
    End Sub
#End Region

#Region "Localizar Textbox"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        If (CBFiltro.Text = "Descrição") Then
            DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, 2)
        End If
    End Sub
#End Region

#Region "Load"
    Private Sub FabricanteUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        carregagrid()
        CBStatus.SelectedIndex = 0
        CBFiltro.SelectedIndex = 0
        CBFiltro.Focus()
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        statustabestoque = "N"
        Dispose()
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "DatagridView Alterar com duplo click"
    Private Sub DGVDado_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        TSBAlterar_Click(Nothing, Nothing)
    End Sub
#End Region

#Region "DatagridView Deletar"
    Private Sub DGVDado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles DGVDados.KeyDown
        If (e.KeyCode = Keys.Delete) Then
            TSBExcluir_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "DatagridView Alterar com enter"
    Private Sub DGVDado_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DGVDados.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TSBAlterar_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

End Class
